package com.timesheet.gui.efforttracker.views;

import java.util.Calendar;

import com.example.timesheet.uitl.ProjectUtil;
import com.example.timesheet.uitl.TaskConstants;
import com.example.timesheet.uitl.TaskUtils;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.models.EffortTrackerPkDTO;
import com.timesheet.serverice.EffortTrackerService;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class FillEffortComposite extends Composite {

	private Text text;

	private Text txtTaskDescrition;

	private Text txtHours;

	private Combo cmbProject;

	private Combo cmbTasktype;

	private Combo cmbTaskphase;

	String taskType = "";

	String taskPhase = "";

	String taskNo = "";

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public FillEffortComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		setBackground(parent.getBackground());
		Label lblProject = new Label(this, SWT.NONE);
		lblProject.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblProject.setText("Project");
		lblProject.setBackground(parent.getBackground());

		cmbProject = new Combo(this, SWT.NONE);
		cmbProject.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cmbProject.setItems(ProjectUtil.getProjects());

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Tasktype");
		lblNewLabel.setBackground(parent.getBackground());

		cmbTasktype = new Combo(this, SWT.NONE);
		cmbTasktype.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cmbTasktype.setItems(TaskUtils.getEffortFillTaskTypes());

		Label lblTaskno = new Label(this, SWT.NONE);
		lblTaskno.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTaskno.setText("Taskno");
		lblTaskno.setBackground(parent.getBackground());

		text = new Text(this, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblTaskphane = new Label(this, SWT.NONE);
		lblTaskphane.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTaskphane.setText("Taskphase");
		lblTaskphane.setBackground(parent.getBackground());

		cmbTaskphase = new Combo(this, SWT.NONE);
		cmbTaskphase.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cmbTaskphase.setItems(TaskUtils.getTaskPhases());

		final Label lblTaskDescritpion = new Label(this, SWT.NONE);
		lblTaskDescritpion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTaskDescritpion.setText("Task Descritpion");
		lblTaskDescritpion.setBackground(parent.getBackground());

		txtTaskDescrition = new Text(this, SWT.BORDER);
		txtTaskDescrition.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label lblDaydate = new Label(this, SWT.NONE);
		lblDaydate.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDaydate.setText("Daydate");
		lblDaydate.setBackground(parent.getBackground());

		final DateTime dateTime = new DateTime(this, SWT.BORDER | SWT.DROP_DOWN);

		Label lblHours = new Label(this, SWT.NONE);
		lblHours.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblHours.setText("Hours");

		txtHours = new Text(this, SWT.BORDER);
		txtHours.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));

		Button btnSave = new Button(this, SWT.NONE);
		btnSave.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		btnSave.setText("&Save");

		cmbTasktype.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if (TaskConstants.FULLDAY_LEAVE.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.FULLDAY_LEAVE;
					taskPhase = TaskConstants.FULLDAY_LEAVE;
					taskNo = "" + TaskConstants.FULLDAY_LEAVE_CODE;
					cmbTaskphase.setEnabled(false);
					text.setEnabled(false);
					text.setText(taskNo);
					txtHours.setText("8");
					txtHours.setEnabled(false);

				}
				else if (TaskConstants.HALFDAY_LEAVE.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.HALFDAY_LEAVE;
					taskPhase = TaskConstants.HALFDAY_LEAVE;
					taskNo = "" + TaskConstants.HALFDAY_LEAVE_CODE;
					cmbTaskphase.setEnabled(false);
					text.setText(taskNo);
					text.setEnabled(false);
					txtHours.setText("4");
					txtHours.setEnabled(false);
				}
				else if (TaskConstants.TEAM_MEETING.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.TEAM_MEETING;
					taskPhase = TaskConstants.TEAM_MEETING;
					taskNo = "" + TaskConstants.TEAM_MEETING_CODE;
					cmbTaskphase.setEnabled(false);
					text.setText(taskNo);
					text.setEnabled(false);
				}
				else if (TaskConstants.ALL_HANDS_MEETING.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.ALL_HANDS_MEETING;
					taskPhase = TaskConstants.ALL_HANDS_MEETING;
					taskNo = "" + TaskConstants.ALL_HANDS_MEETING_CODE;
					cmbTaskphase.setEnabled(false);
					text.setText(taskNo);
					text.setEnabled(false);
				}
				else if (TaskConstants.MACHINE_SETUP.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.MACHINE_SETUP;
					taskPhase = TaskConstants.MACHINE_SETUP;
					taskNo = "" + TaskConstants.MACHINE_SETUP_CODE;
					cmbTaskphase.setEnabled(false);
					text.setText(taskNo);
					text.setEnabled(false);
				}
				else if (TaskConstants.SCRUM.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.SCRUM;
					taskPhase = TaskConstants.SCRUM;
					taskNo = "" + TaskConstants.SCRUM_CODE;
					cmbTaskphase.setEnabled(false);
					text.setText(taskNo);
					text.setEnabled(false);
				}
				else {
					taskType = cmbTasktype.getText();
					taskPhase = cmbTaskphase.getText();
					taskNo = text.getText();
					cmbTaskphase.setEnabled(true);
					text.setText(taskNo);
					text.setEnabled(true);
					txtHours.setText("");
					txtHours.setEnabled(true);

				}
			}
		});

		btnSave.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				EffortTrackerService service = new EffortTrackerService();
				EffortTrackerDTO cEffortTrackerDto = new EffortTrackerDTO();
				EffortTrackerPkDTO effortTrackerPk = new EffortTrackerPkDTO();

				//effortTrackerPk.setEmployeeId(Session.INSTANCE.getUser());
				//effortTrackerPk.setProjectId("ABSLIFE");

				if (TaskConstants.FULLDAY_LEAVE.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.FULLDAY_LEAVE;
					taskPhase = TaskConstants.FULLDAY_LEAVE;
					taskNo = "" + TaskConstants.FULLDAY_LEAVE_CODE;

				}
				else if (TaskConstants.HALFDAY_LEAVE.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.HALFDAY_LEAVE;
					taskPhase = TaskConstants.HALFDAY_LEAVE;
					taskNo = "" + TaskConstants.HALFDAY_LEAVE_CODE;

				}
				else if (TaskConstants.TEAM_MEETING.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.TEAM_MEETING;
					taskPhase = TaskConstants.TEAM_MEETING;
					taskNo = "" + TaskConstants.TEAM_MEETING_CODE;
					cmbTasktype.setEnabled(false);
				}
				else if (TaskConstants.ALL_HANDS_MEETING.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.ALL_HANDS_MEETING;
					taskPhase = TaskConstants.ALL_HANDS_MEETING;
					taskNo = "" + TaskConstants.ALL_HANDS_MEETING_CODE;

				}
				else if (TaskConstants.MACHINE_SETUP.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.MACHINE_SETUP;
					taskPhase = TaskConstants.MACHINE_SETUP;
					taskNo = "" + TaskConstants.MACHINE_SETUP_CODE;

				}
				else if (TaskConstants.SCRUM.equals(cmbTasktype.getText())) {
					taskType = TaskConstants.SCRUM;
					taskPhase = TaskConstants.SCRUM;
					taskNo = "" + TaskConstants.SCRUM_CODE;

				}
				else {
					taskType = cmbTasktype.getText();
					taskPhase = cmbTaskphase.getText();
					taskNo = text.getText();

				}

				cEffortTrackerDto.setTaskDescription(txtTaskDescrition.getText());
				//effortTrackerPk.setTaskNo(taskNo);
				//effortTrackerPk.setTaskType(taskType);
				//effortTrackerPk.setTaskPhase(taskPhase);

				int day = dateTime.getDay();
				int month = dateTime.getMonth();
				int year = dateTime.getYear();

				Calendar cal = Calendar.getInstance();
				cal.set(year, month, day);

				//effortTrackerPk.setDayDate(cal.getTime());

				cEffortTrackerDto.setHours(new Double(txtHours.getText()));

				//cEffortTrackerDto.setEffortTrackerPkDTO(effortTrackerPk);

				try {
					service.fillEfforts(cEffortTrackerDto);
				}
				catch (Exception exception) {
					exception.printStackTrace();
				}
			}
		});

		Button btnUpdate = new Button(this, SWT.NONE);
		GridData gd_btnUpdate = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnUpdate.widthHint = 68;
		btnUpdate.setLayoutData(gd_btnUpdate);
		btnUpdate.setText("&Reset");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
